package com.example.qzx.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        setContentView(new CustomView2(this));
    }



    /**
     * 使用内部类 自定义一个简单的View
     * @author Administrator
     *
     */
    class CustomView1 extends View {

        Paint paint;

        public CustomView1(Context context) {
            super(context);//这里是初始化父类的构造方法


            paint = new Paint(); //设置一个笔刷大小是3的黄色的画笔
            paint.setColor(Color.YELLOW);//设置画笔颜色
            paint.setStrokeJoin(Paint.Join.ROUND);//ROUND
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStrokeWidth((float) 3.0);//线宽
            paint.setStyle(Paint.Style.FILL);//空心效果STROKE   实心FILL
            paint.setAntiAlias(false);//设置画笔为无锯齿
        }

        //在这里我们将测试canvas提供的绘制图形方法
        @Override
        protected void onDraw(Canvas canvas) {

            canvas.drawColor(Color.BLUE);
     /*       //================================


            //================================
            paint = new Paint(); //设置一个笔刷大小是3的黄色的画笔
            paint.setColor(Color.YELLOW);//设置画笔颜色
            canvas.drawCircle(100, 100, 100, paint);
            //弧线(arcs)、填充颜色(argb和color)、 Bitmap、圆(circle和oval)、点(point)、线(line)、矩形(Rect)、图片(Picture)、圆角矩形 (RoundRect)、文本(text)、顶点(Vertices)、路径(path)
            paint.setStrokeWidth((float) 3.0);
            canvas.drawColor(Color.green(1));                //白色背景
            canvas.drawCircle(50, 50, 10, paint);           //绘制圆形
            canvas.drawCircle(100, 100, 20, paint);         //绘制圆形
            canvas.drawCircle(150, 150, 30, paint);         //绘制圆形
            canvas.drawCircle(200, 200, 40, paint);         //绘制圆形
            canvas.drawCircle(250, 250, 50, paint);         //绘制圆形
            canvas.drawCircle(300, 300, 60, paint);         //绘制圆形
            canvas.drawCircle(350, 350, 70, paint);         //绘制圆形
            //================================
            paint = new Paint(); //设置一个笔刷大小是3的黄色的画笔
            paint.setColor(Color.RED);//设置画笔颜色
            paint.setAntiAlias(false);//设置画笔为无锯齿
            //绘制弧线区域
            RectF rect = new RectF(0, 0, 200, 200);
            canvas.drawArc(rect, //弧线所使用的矩形区域大小
                    0,  //开始角度
                    90, //扫过的角度
                    true, //是否使用中心
                    paint);*/
            //================================

            //  canvas.drawLine(20,40,200,40,paint);//自定义一条横线

            //================================
   /*         //定义一个矩形区域
            RectF oval = new RectF(0,0,200,300);
            //矩形区域内切椭圆
            canvas.drawOval(oval, paint);
*/
            //=================================
            //按照既定点 绘制文本内容
/*            canvas.drawPosText("Android777", new float[]{
                    10,10, //第一个字母在坐标10,10
                    20,20, //第二个字母在坐标20,20
                    30,30, //....
                    40,40,
                    50,50,
                    60,60,
                    70,70,
                    80,80,
                    90,90,
                    100,100
            }, paint);*/
            //==================================
            //绘制一个长方形
           /* RectF rect = new RectF(50, 50, 200, 400);
            canvas.drawRect(rect, paint);*/


            //==================================
            //绘制一个钝化四脚的正方形
/*            RectF rect = new RectF(50, 50, 200, 200);
            canvas.drawRoundRect(rect,
                    30, //x轴的半径
                    30, //y轴的半径
                    paint);*/

            //=====================================
 /*           Path path = new Path(); //定义一条路径
            path.moveTo(10, 10); //移动到 坐标10,10
            path.lineTo(50, 60);
            path.lineTo(200,80);
            path.lineTo(10, 10);
            canvas.drawPath(path, paint);
*/
            //=====================================
           /* Path path = new Path(); //定义一条路径
            path.moveTo(10, 10); //移动到 坐标10,10
            path.lineTo(50, 60);
            path.lineTo(200,80);
            path.lineTo(10, 10);

            canvas.drawPath(path, paint);
            canvas.drawTextOnPath("Android777开发者博客", path, 10, 10, paint);*/
            //=======================================
           /* paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.STROKE);
            canvas.translate(canvas.getWidth()/2, 200); //将位置移动画纸的坐标点:150,150
            canvas.drawCircle(0, 0, 100, paint); //画圆圈

            //使用path绘制路径文字
            canvas.save();
            canvas.translate(-75, -75);
            Path path = new Path();
            path.addArc(new RectF(0,0,150,150), -180, 180);
            Paint citePaint = new Paint(paint);
            citePaint.setTextSize(14);
            citePaint.setStrokeWidth(1);
            canvas.drawTextOnPath("http://www.android777.com", path, 28, 0, citePaint);
            canvas.restore();

            Paint tmpPaint = new Paint(paint); //小刻度画笔对象
            tmpPaint.setStrokeWidth(1);

            float  y=100;
            int count = 60; //总刻度数

            for(int i=0 ; i <count ; i++){
                if(i%5 == 0){
                    canvas.drawLine(0f, y, 0, y+12f, paint);
                    canvas.drawText(String.valueOf(i/5+1), -4f, y+25f, tmpPaint);

                }else{
                    canvas.drawLine(0f, y, 0f, y +5f, tmpPaint);
                }
                canvas.rotate(360/count,0f,0f); //旋转画纸
            }

            //绘制指针
            tmpPaint.setColor(Color.GRAY);
            tmpPaint.setStrokeWidth(4);
            canvas.drawCircle(0, 0, 7, tmpPaint);
            tmpPaint.setStyle(Paint.Style.FILL);
            tmpPaint.setColor(Color.YELLOW);
            canvas.drawCircle(0, 0, 5, tmpPaint);
            canvas.drawLine(0, 10, 0, -65, paint);*/

            //======================================


        }
    }


        /**
         * 使用内部类 自定义一个简单的View
         * @author Administrator
         *
         */
        class CustomView2 extends View{

            Paint paint;
            private ArrayList<PointF> graphics = new ArrayList<PointF>();
            public CustomView2(Context context) {
                super(context);
                paint = new Paint(); //设置一个笔刷大小是3的黄色的画笔
                paint.setColor(Color.YELLOW);
                paint.setStyle(Paint.Style.FILL);//空心效果STROKE   实心FILL
                paint.setStrokeJoin(Paint.Join.ROUND);
                paint.setStrokeCap(Paint.Cap.ROUND);
                paint.setStrokeWidth(3);

            }

            @Override
            public boolean onTouchEvent(MotionEvent event) {

                graphics.add(new PointF(event.getX(),event.getY()));

                invalidate(); //重新绘制区域

                return true;
            }

            //在这里我们将测试canvas提供的绘制图形方法
            @Override
            protected void onDraw(Canvas canvas) {
                canvas.drawColor(Color.BLUE);
                for (PointF point : graphics) {
                    canvas.drawPoint(point.x, point.y, paint);
                }
//          super.onDraw(canvas);

            }
        }





}
